package com.neusoft.Shixun.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.Shixun.dao.CareItemDao;
import com.neusoft.Shixun.dao.LevelItemDao;
import com.neusoft.Shixun.po.CareItem;
import com.neusoft.Shixun.po.LevelItem;
import com.neusoft.Shixun.po.ResponseBean;
import com.neusoft.Shixun.service.LevelItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class LevelItemServiceImpl implements LevelItemService {

    @Autowired
    private LevelItemDao levelItemDao;

    @Autowired
    private CareItemDao careItemDao;

    @Override
    public ResponseBean<List<LevelItem>> getAllLevelItems() {
        List<LevelItem> levelItems = levelItemDao.selectList(null);
        return new ResponseBean<>(levelItems);
    }

    @Override
    public ResponseBean<LevelItem> getLevelItemById(Integer levelItemId) {
        LevelItem levelItem = levelItemDao.selectById(levelItemId);
        if (levelItem != null) {
            return new ResponseBean<>(levelItem);
        } else {
            return new ResponseBean<>(404, "护理级别与项目关系未找到");
        }
    }

    @Override
    public ResponseBean<Integer> addLevelItem(LevelItem levelItem) {
        int result = levelItemDao.insert(levelItem);
        return new ResponseBean<>(result); // 返回插入成功的行数
    }

    @Override
    public ResponseBean<Integer> updateLevelItem(LevelItem levelItem) {
        int result = levelItemDao.updateById(levelItem);
        return new ResponseBean<>(result); // 返回更新成功的行数
    }

    @Override
//    删除护理级别下的项目
    public ResponseBean<Integer> deleteLevelItem(Integer levelId, Integer itemId) {
        LevelItem levelItem = levelItemDao.selectOne(new QueryWrapper<LevelItem>().eq("level_id", levelId).eq("item_id", itemId));
        int result = levelItemDao.deleteById(levelItem.getLevelItemId());
        return new ResponseBean<>(result); // 返回删除成功的行数
    }

    @Override
//    查询该护理级别下的项目
    public ResponseBean<List<CareItem>> getCareItemsByLevelId(Integer levelId) {
        List<LevelItem> result = levelItemDao.selectList(new QueryWrapper<LevelItem>().eq("level_id", levelId));
        List<CareItem> careItems = new ArrayList<>();
        for (LevelItem levelItem : result) {
            CareItem careItem = careItemDao.selectById(levelItem.getItemId());
            if (careItem.getStatus().equals("停用") || (careItem.getIsActive() != null && careItem.getIsActive().equals("移除"))) {
                continue;
            }
            careItems.add(careItem);
        }
        if (careItems.size() > 0) {
            return new ResponseBean<>(careItems);
        } else {
            return new ResponseBean<>(404, "未找到该级别的项目");
        }
    }

    @Override
//    查询不在该护理级别下的项目
    public ResponseBean<List<CareItem>> getCareItemsNotBelongsToLevelId(Integer levelId) {
//       在护理级别的项目
        List<LevelItem> result = levelItemDao.selectList(new QueryWrapper<LevelItem>().eq("level_id", levelId));
        Set<CareItem> careItems = new HashSet<>();
        for (LevelItem levelItem : result) {
            CareItem careItem = careItemDao.selectById(levelItem.getItemId());
            careItems.add(careItem);
        }
//        查询所有项目
        List<CareItem> allCareItems = careItemDao.selectList(
                new QueryWrapper<CareItem>()
                        .eq("status", "启用")
                        .and(wrapper -> wrapper.ne("is_active", "移除").or().isNull("is_active"))
        );

        List<CareItem> notBelongsToLevelItems = new ArrayList<>();
        for (CareItem careItem : allCareItems) {
            boolean belongsToLevel = false;
            for (CareItem itemInLevel : careItems) {
                if (itemInLevel.getItemId().equals(careItem.getItemId())) { // 比较 id 字段
                    belongsToLevel = true;
                    break;
                }
            }
            if (!belongsToLevel) {
                notBelongsToLevelItems.add(careItem);
            }
        }

        if (!notBelongsToLevelItems.isEmpty()) {
            return new ResponseBean<>(notBelongsToLevelItems);
        } else {
            return new ResponseBean<>(404, "未找不属于该级别的项目");
        }
    }

    @Override
//    添加某个护理项目到护理级别
    public ResponseBean<Integer> addCareItemToLevelItem(Integer careLevelId, Integer careItemId) {
//        先查询有没有该条记录,如果有应该只有一条
        LevelItem levelItem = levelItemDao.selectOne(new QueryWrapper<LevelItem>().eq("level_id", careLevelId).eq("item_id", careItemId));
        if (levelItem != null) {
            return new ResponseBean<>(404, "该项目已经在护理级别中");
        } else {
            LevelItem newLevelItem = new LevelItem();
            newLevelItem.setLevelId(careLevelId);
            newLevelItem.setItemId(careItemId);
            int result = levelItemDao.insert(newLevelItem);
            return new ResponseBean<>(result);
        }
    }


}